package com.gsmvn.ngamgaixinh;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.appota.congaixinh.model.Banner;
import com.appota.vn.social.common.CommonStatic;
import com.google.gson.Gson;

public class JsonUtil {

	public static String getAccessToken(String str) {
		String result = "";
		try {
			JSONObject jsonObject = new JSONObject(str);
			String state = jsonObject.getString(CommonStatic.STATUS);
			if (state.equals("true")) {
				result = jsonObject.getString(CommonStatic.ACCESS_TOKEN);
			} else {

			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}
	public static final String TAG = "JsonUtil";

	public static int getErrorCode(String str) {

		try {
			JSONObject jsonObject = new JSONObject(str);
			return jsonObject.getInt(CommonStatic.ERROR_CODE);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	public static boolean getStatus(String str) {
		try {
			JSONObject jsonObject = new JSONObject(str);
			Boolean status = jsonObject.getBoolean(CommonStatic.STATUS);
			return status;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public static String getErrorMessage(String str) {
		try {
			JSONObject jsonObject = new JSONObject(str);
			String err_msg = jsonObject.getString(CommonStatic.MESSAGE);
			return err_msg;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static Banner getBanner(String str) {
		try {
			JSONObject jsonObject = new JSONObject(str);
			Boolean status = jsonObject.getBoolean(CommonStatic.STATUS);
			if (status) {
				JSONArray data = jsonObject.getJSONArray("data");
				JSONObject bannerJson = data.getJSONObject(0);
				Gson gson = new Gson();
				Banner banner = gson .fromJson(bannerJson.toString(), Banner.class);
				return banner;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static boolean followUser(String str) {
		try {
			JSONObject jsonObject = new JSONObject(str);
			Boolean status = jsonObject.getBoolean(CommonStatic.STATUS);
			return status;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public static boolean unfollowUser(String str) {
		try {
			JSONObject jsonObject = new JSONObject(str);
			Boolean status = jsonObject.getBoolean(CommonStatic.STATUS);
			return status;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static String getRequestToken(String str) {
		String result = "";
		try {
			JSONObject jsonObject = new JSONObject(str);
			String state = jsonObject.getString(CommonStatic.STATUS);
			if (state.equals("true")) {
				result = jsonObject.getString(CommonStatic.REQUEST_TOKEN);
			} else {

			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}


	
}
